home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Code Resources / 3D Buttons CDEF 1.0b4 / Source / 3D Buttons CDEF source / (3D Buttons CDEF.π) / LGBControl.cp < prev    next >
Encoding:
Text File  |  1994-07-04  |  3.1 KB  |  93 lines  |  [TEXT/MMCC]

  1. /**************************************************************************
  2.     LGBControl
  3.     
  4.     Public domain, by Zig Zichterman.
  5.     
  6.     Just a few utilities that the control classes use.
  7.     
  8.     Some of the drawing code is taken from the public domain source
  9.     accompanying _develop_ 15.
  10. **************************************************************************/
  11. #include "LGBControl.h"
  12.  
  13. const LGBControl_boxWidth    = 12;    // width of checkboxes, radio buttons
  14.  
  15. //—————————————————————————————————————————————————————————————————————————
  16. // Drawing Utilities
  17. //—————————————————————————————————————————————————————————————————————————
  18.  
  19. /**************************************************************************
  20.     CountLines()                                                [static]
  21.     
  22.     Count the number of carriage return ('\r') characters in the
  23.     given string.
  24.     
  25.     carriage return is NEVER part of a 2-byte character, so this will
  26.     work with double byte scripts, too.
  27. **************************************************************************/
  28. short
  29. LGBControl::CountLines(const StringPtr inString)
  30. {
  31.     short            numLines        = 1;    // always have at least 1 line
  32.     short            charsRemaining    = *inString;
  33.     unsigned char    *p                = inString + 1;
  34.     for (;charsRemaining--;p++) {
  35.         if (*p == '\r') numLines++;
  36.     }
  37.     return numLines;
  38. }
  39.  
  40. /**************************************************************************
  41.     TitleHeight()                                                [static]
  42.     
  43.     Return the height of the title in pixels. The font must already be
  44.     set up.
  45. **************************************************************************/
  46. short
  47. LGBControl::TitleHeight(const StringPtr inTitle)
  48. {
  49.     FontInfo    info;
  50.     ::GetFontInfo(&info);
  51.     const short    lineHeight    = info.ascent + info.descent + info.leading;
  52.     const short    numLines    = CountLines(inTitle);
  53.     return lineHeight * numLines;
  54. }
  55.  
  56. /**************************************************************************
  57.     CalcBoxes()                                                    [static]
  58.     
  59.     Calculate the boxes for the checkbox/radio button and its title
  60. **************************************************************************/
  61. void
  62. LGBControl::CalcBoxes(const Rect &inControlRect, const StringPtr inTitle,
  63.         Rect &outCheckbox, Rect &outTitleBox)
  64. {
  65.     short        centerV        = (inControlRect.top + inControlRect.bottom)/2;
  66.     Boolean        ltor        = (GetSysJust() == 0);
  67.     
  68.     // checkbox is v-centered, 14-px tall
  69.     outCheckbox.top        = centerV - (LGBControl_boxWidth/2);
  70.     outCheckbox.bottom    = outCheckbox.top + LGBControl_boxWidth;
  71.     // checkbox is left- or right-aligned, offset by a couple pix
  72.     if (ltor) {
  73.         outCheckbox.left    = inControlRect.left + 2;
  74.         outCheckbox.right    = outCheckbox.left + LGBControl_boxWidth;
  75.     } else {
  76.         outCheckbox.right    = inControlRect.right - 2;
  77.         outCheckbox.left    = outCheckbox.right - LGBControl_boxWidth;
  78.     }
  79.     
  80.     // textbox is v-centered, tall enough for all lines
  81.     const short        titleHeight    = LGBControl::TitleHeight(inTitle);
  82.     outTitleBox.top                = centerV - (titleHeight/2);
  83.     outTitleBox.bottom    = outTitleBox.top + titleHeight;
  84.     // textbox is next to the checkbox
  85.     if (ltor) {
  86.         outTitleBox.left    = outCheckbox.right + 3;
  87.         outTitleBox.right    = inControlRect.right;
  88.     } else {
  89.         outTitleBox.right    = outCheckbox.left - 3;
  90.         outTitleBox.left    = inControlRect.left;
  91.     }
  92. }
  93.